<template>
  <div class="JNPF-common-layout">
    <div class="JNPF-common-layout-center">
      <el-row class="JNPF-common-search-box" :gutter="16">
        <el-form @submit.native.prevent label-width="90px" label-position="left">
          <el-col :span="8">
            <el-form-item label="客户名称" >
              <el-select
                :disabled='$store.state.user.fsiteLevel == 3'
                @change="fsiteNoQueChange"
                v-model="listQuery.fsiteNo"
                clearable
                filterable
                class="filter-item"
                placeholder="请选择客户名称"
              >
                <el-option
                  v-for="item in getListfsiteNo"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="存放地点">
                  <el-select
                  @focus="fpointNoFocus"
                  @change="fpointNoChange"
                  clearable
                  filterable
                  v-model="listQuery.fpointNo"
                  placeholder="请选择存放地点"
                >
                  <el-option
                    v-for="item in getListfpointNoQuery"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select>
            </el-form-item>
          </el-col>
          <template v-if="showAll">
            <el-col :span="8">
            <el-form-item label="存放位置">
          <el-input
            v-model="listQuery.fpointSize"
            clearable
            class="filter-item"
            placeholder="请输入存放位置"
          />
        </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="设备类型">
          <el-select
            v-model="listQuery.fequipmenttype"
            filterable
            clearable
            class="filter-item"
            placeholder="请选择设备分类"
          >
            <el-option
              v-for="item in getListfequipmenttype"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="序列号">
          <el-input
            v-model="listQuery.fequipmentbarcode"
            clearable
            class="filter-item"
            placeholder="请输入序列号"
          />
        </el-form-item>
          </el-col>
          <el-col :span="8">
             <el-form-item label="设备标签">
          <el-input
            v-model="listQuery.fequipmenttag"
            clearable
            class="filter-item"
            placeholder="请输入设备标签"
          />
        </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="固定资产码">
          <el-input
            v-model="listQuery.fmacno"
            clearable
            class="filter-item"
            placeholder="请输入固定资产码"
          />
        </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="设备状态">
          <el-select
            v-model="listQuery.fstate"
            clearable
            filterable
            placeholder="请选择设备状态"
          >
            <el-option
              v-for="item in fstateOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
          </el-col>

          </template>
          <el-col :span="8">
            <el-form-item>
              <el-button type="primary"
          @click="handleFilter"
          >查找</el-button
        >
        <el-button
        type="success"
          @click="handleCreate"
          >添加</el-button
        >
        <el-button type="text" icon="el-icon-arrow-down" @click="showAll=true"
                v-if="!showAll">展开</el-button>
              <el-button type="text" icon="el-icon-arrow-up" @click="showAll=false" v-else>
                收起</el-button>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div class="JNPF-common-layout-main JNPF-flex-main">
       <!-- 查询结果 -->
        <JNPF-table
          v-loading="listLoading"
          :data="list"
        >
          <el-table-column
            align="center"
            :width="buttonColor.operationTwo"
            label="操作"
            class-name="small-padding"
          >
            <template slot-scope="scope">
              <el-button type="text" @click="handleUpdate(scope.row)">编辑</el-button>
              <el-button type="text" @click="handleDelete(scope.row)">删除</el-button>
            </template>
          </el-table-column>
          <el-table-column
            :width="buttonColor.line"
            :index="indexMethod"
            align="center"
            label="序"
            type="index"
          />
          <el-table-column
            :formatter="formatfsiteNo"
            align="left"
            label="客户名称"
            prop="fsiteNo"
          />
          <el-table-column
            :formatter="formatfpointNo"
            align="left"
            label="存放地点"
            prop="fpointNo"
          />
          <el-table-column align="left" label="存放位置" prop="fpointSize" />
          <el-table-column align="left" label="序列号" prop="fequipmentbarcode" width="180"/>
          <el-table-column align="left" label="固定资产码" prop="fmacno" />
          <el-table-column align="left" label="设备标签" prop="fequipmenttag" />
          <el-table-column align="left" label="排插MAC" prop="flinkmac" />
          <el-table-column align="left" label="标识符" prop="flinkmark" />
        </JNPF-table>
        <paginations
          :total="total"
          :page.sync="listQuery.currentPage"
          :limit.sync="listQuery.pageSize"
          />
      </div>
    </div>
    <el-dialog
      width="80%"
      top="10px"
      :close-on-click-modal="false"
      :visible.sync="dialogFormVisible"
      :before-close="handleClose"
      :title="textMap[dialogStatus]"
    >
    <el-row class="JNPF-common-search-box" :gutter="16">
        <el-form  ref="dataForm" :rules="rules" :model="dataForm" label-width="90px">
          <el-col :span="8">
            <el-form-item
              label="客户名称"
              prop="fsiteNo"
            >
              <el-select
                @change="fsiteNoChange"
                v-model="dataForm.fsiteNo"
                clearable
                filterable
                :disabled=" dialogStatus === 'create' &&$store.state.user.fsiteLevel !== 3"
                class="filter-item"
                placeholder="请选择用户名称"
              >
                <el-option
                  v-for="item in getListfsiteNo"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="存放地点" prop="fpointNo">
              <el-select
                  style="display: inline-block"
                  clearable
                  filterable
                  v-model="dataForm.fpointNo"
                  class="filter-item"
                  placeholder="请选择存放地点"
                >
                  <el-option
                    v-for="item in getListfpointNo"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="存放位置" prop="fpointSize">
              <el-input v-model="dataForm.fpointSize" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item
              label="设备编号"
              prop="feuipmentid"
            >
              <el-input
              :disabled="dialogStatus != 'create'"
                  v-model="dataForm.feuipmentid"
                  style="display: inline-block"
                />
            </el-form-item>
          </el-col>
          <el-col :span="8">
             <el-form-item
              label="序列号"
              prop="fequipmentbarcode"
            >
              <el-input :disabled="true" v-model="dataForm.fequipmentbarcode" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="固定资产码" prop="fmacno">
              <el-input v-model="dataForm.fmacno" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="设备标签" prop="fequipmenttag">
              <el-input v-model="dataForm.fequipmenttag" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="设备类型" prop="fequipmenttype">
              <el-select
                :disabled="true"
                v-model="dataForm.fequipmenttype"
                filterable
                clearable
                class="filter-item"
                placeholder="请选择设备分类"
              >
                <el-option
                  v-for="item in getListfequipmenttype"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item  label="设备状态" prop="fstate">
              <el-select
                v-model="dataForm.fstate"
                clearable
                filterable
                placeholder="请选择设备状态"
              >
                <el-option
                  v-for="item in fstateOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="安装日期" prop="finstalldate">
              <el-date-picker v-model="dataForm.finstalldate" type="date" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="下次保养" prop="fmaintaindate">
              <el-date-picker v-model="dataForm.fmaintaindate" type="date" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="最后维修" prop="frepairdate">
              <el-date-picker v-model="dataForm.frepairdate" type="date" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="设备原值" prop="fbuyprice">
              <el-input v-model="dataForm.fbuyprice" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="折旧方法" prop="foldway">
              <el-select
                clearable
                filterable
                v-model="dataForm.foldway"
                placeholder="请选择折旧方法"
              >
                <el-option
                  v-for="item in foldwayOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="累加折旧" prop="foldmoney">
              <el-input v-model="dataForm.foldmoney" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="净残值率" prop="flastrate">
              <el-input v-model="dataForm.flastrate" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="净值" prop="flastmoney">
              <el-input v-model="dataForm.flastmoney" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="报废日期" prop="flosdate">
              <el-date-picker v-model="dataForm.flosdate" type="date" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="排插MAC" prop="flinkmac">
              <el-input v-model="dataForm.flinkmac" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="标识符" prop="flinkmark">
              <el-input v-model="dataForm.flinkmark" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="面板颜色" prop="fcolor">
              <el-input v-model="dataForm.fcolor" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="TCP/IP" prop="fip">
              <el-input v-model="dataForm.fip" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="PORT" prop="fport">
              <el-input v-model="dataForm.fport" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="供应商" prop="fsupno">
              <el-input v-model="dataForm.fsupno" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否预约" prop="fmeeting">
              <el-select
                clearable
                filterable
                v-model="dataForm.fmeeting"
                placeholder="请选择是否预约"
              >
                <el-option
                  v-for="item in yuyueList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item
              class="text"
              style="width: 100%"
              label="视频URL"
              prop="fvideourl"
            >
              <el-input v-model="dataForm.fvideourl" />
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible=false" type="info">取消</el-button>
        <el-button v-if="dialogStatus == 'create'" type="primary" @click="createData"
          >确定</el-button
        >
        <el-button v-else @click="updateData" type="primary">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  listequipmentInv,
  createequipmentInv,
  updateequipmentInv,
  deleteequipmentInv,
  uploadPath,
} from "@/api/equipmentInv";
import {
  fsiteOptionsByLevel,
  getListArea,
  getFequipmentbarcode,
  getPointOptions,
} from "@/api/area";
import { listinfo } from "@/api/info";
import Pagination from "@/components/Pagination";
import moment from "moment";
import { tableColor, titleColor, buttonColor } from "@/filters/index";
import { getToken } from "@/utils/auth";
import { listRoom } from "@/api/room";
import QRcode from "@chenfengyuan/vue-qrcode";

const statusMap = {
  0: "运行",
  1: "待保养",
  2: "待维修",
};

const foldwayMap = {
  0: "平均年限法",
  1: "不计提折旧",
};

export default {
  name: "TmpEquipmentInv",
  components: { Pagination, qrcode: QRcode },
  filters: {
    statusFilter(type) {
      return statusMap[type];
    },
    foldwayFilter(type) {
      return foldwayMap[type];
    },
  },
  data() {
    return {
      yuyueList: [
        { label: "否", value: 0 },
        { label: "是", value: 1 },
      ],
      showAll:false,
      buttonColor: buttonColor,
      list2: null,
      isPrint: true,
      printed: false,
      uploadPath,
      getFsiteNo: null,
      selectListlab: [],
      searchVisiblelab: false,
      searchListlab: [],
      listLoadingSearchlab: true,
      totalSearchlab: 0,
      listQuerySearchlab: {
        page: 1,
        limit: 20,
        fpointName: undefined,
        sort: "create_time",
        order: "desc",
      },

      searchFlaglab: "",

      fstateOptions: null,
      getListfclassstno: null,
      getListfequipmenttype: null,
      getListfsiteNo: [],
      getListfpointNo: [],
      getListfpointNoQuery: null,
      getListfpointNoAll: null,
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        fstate: undefined,
        fsiteNo: undefined,
        feuipmentid: undefined,
        fequipmenttag: undefined,
        fpointNo: undefined,
        fpointSize: undefined,
        finstalldate: undefined,
        fequipmenttype: undefined,
        fequipmentbarcode: undefined,
        flinkmac: undefined,
        fmacno: undefined,
        sort: "create_time",
        order: "desc",
      },
      searchListAll: [],
      searchList: [],
      searchVisible: false,
      totalSearch: 0,
      listLoadingSearch: true,
      listQuerySearch: {
        page: 1,
        limit: 20,
        fequipmentno: undefined,
        sort: "create_time",
        order: "desc",
      },
      foldwayOptions: [
        {
          value: 0,
          label: "平均年限法",
        },
        {
          value: 1,
          label: "不计提折旧",
        },
      ],
      dataForm: {
        fsiteNo: undefined,
        feuipmentid: undefined,
        fequipmenttag: undefined,
        fpointNo: undefined,
        fpointSize: undefined,
        finstalldate: undefined,
        fequipmenttype: undefined,
        fequipmentbarcode: undefined,
        fstate: undefined,
        fmaintaindate: undefined,
        frepairdate: undefined,
        fbuyprice: undefined,
        foldway: undefined,
        foldmoney: undefined,
        flastrate: undefined,
        flastmoney: undefined,
        flosdate: undefined,
        fvideourl: undefined,
        fmacno: undefined,
        fsupno: undefined,
        flinkmac: undefined,
        flinkmark: undefined,
        fip: undefined,
        fport: undefined,
        fcolor: undefined,
        fmeeting: undefined,
        id: undefined,
      },
      dialogFormVisible: false,
      dialogStatus: "",
      textMap: {
        update: "编辑",
        create: "创建",
      },
      tableColor: tableColor,
      titleColor: titleColor,
      rules: {
        fsiteNo: [{ required: true, message: "用户编号不能为空" }],
        feuipmentid: [{ required: true, message: "设备编号不能为空" }],
        fstate: [{ required: true, message: "设备状态不能为空" }],
        fequipmentbarcode: [{ required: true, message: "序列号不能为空" }],
      },
    };
  },
  computed: {
    headers() {
      return {
        Authorization: getToken(),
      };
    },
  },
  created() {
    if (this.$store.state.user.fsiteLevel === 3) {
      this.listQuery.fsiteNo = this.$store.state.user.fsiteNo;
      getPointOptions(this.$store.state.user.fsiteNo)
        .then((response) => {
          this.getListfpointNoQuery = response.data.data.list;
        })
        .catch(() => {
          this.getListfpointNoQuery = [];
        });
    }
    this.getList();
    fsiteOptionsByLevel(3)
      .then((response) => {
        this.getListfsiteNo = response.data.data.list;
      })
      .catch(() => {
        this.getListfsiteNo = [];
      });

    this.getSearchList();
    getListArea({ flag: 2 })
      .then((response) => {
        this.getListfclassstno = response.data.data.list;
      })
      .catch(() => {
        this.getListfclassstno = [];
      });

    getListArea({ flag: 1 })
      .then((response) => {
        this.getListfequipmenttype = response.data.data.list;
      })
      .catch(() => {
        this.getListfequipmenttype = [];
      });

    // 存放地点
    getPointOptions()
      .then((response) => {
        this.getListfpointNoAll = response.data.data.list;
      })
      .catch(() => {
        this.getListfpointNoAll = [];
      });

    getListArea({ flag: 22 })
      .then((response) => {
        this.fstateOptions = response.data.data.list;
      })
      .catch(() => {
        this.fstateOptions = [];
      });

    // 全部设备列表
    listinfo()
      .then((response) => {
        this.searchListAll = response.data.data.list;
      })
      .catch(() => {
        this.searchListAll = [];
      });
  },
  methods: {
    formatSite2: function (number) {
      for (let i = 0; i < this.getListfsiteNo.length; i++) {
        if (number === this.getListfsiteNo[i].value) {
          return this.getListfsiteNo[i].label;
        }
      }
      return "";
    },
    formatfsiteNo: function (row, column) {
      if (this.getListfsiteNo) {
        for (let i = 0; i < this.getListfsiteNo.length; i++) {
          if (row.fsiteNo === this.getListfsiteNo[i].value) {
            return this.getListfsiteNo[i].label;
          }
        }
        return "";
      } else {
        return "";
      }
    },
    handleClose(done) {
      this.listQuerySearchlab.page = 1;
      done();
    },
    fsiteNoChange(data) {
      this.dataForm.fpointNo = "";
      this.searchListlab = [];
      this.getListfpointNo = [];
      if (data) {
        this.getFsiteNo = data;
        this.getSearchListlab(data);
        getPointOptions(data)
          .then((response) => {
            this.getListfpointNo = response.data.data.list;
          })
          .catch(() => {
            this.getListfpointNo = [];
          });
      }
    },
    fsiteNoQueChange(data) {
      this.listQuery.fpointNo = undefined;
      this.searchListlab = [];
      this.getListfpointNoQuery = [];
      this.printed = false;
      this.isPrint = true;
      if (data) {
        this.getFsiteNo = data;
        getPointOptions(data)
          .then((response) => {
            this.getListfpointNoQuery = response.data.data.list;
          })
          .catch(() => {
            this.getListfpointNoQuery = [];
          });
      }
    },
    // 地点获取搜索列表
    getSearchListlab(data) {
      this.listLoadingSearchlab = true;
      if (data) {
        const temp = {
          fsiteNo: data,
          fpointFlag: 4,
          page: this.listQuerySearchlab.page,
          limit: this.listQuerySearchlab.limit,
          sort: "create_time",
          order: "desc",
          fpointName: this.listQuerySearchlab.fpointName,
        };
        listRoom(temp)
          .then((response) => {
            this.searchListlab = response.data.data.list;
            this.totalSearchlab = response.data.data.total;
            this.listLoadingSearchlab = false;
          })
          .catch(() => {
            this.searchListlab = [];
            this.totalSearchlab = 0;
            this.listLoadingSearchlab = false;
          });
      } else {
        this.searchListlab = [];
      }
    },

    // 地点获取搜索列表
    getSearchListlabPage(data) {
      this.listLoadingSearchlab = true;
      let queryFsiteNo;
      if (data) {
        if (this.searchFlaglab === "add") {
          queryFsiteNo = this.dataForm.fsiteNo;
        } else {
          queryFsiteNo = this.listQuery.fsiteNo;
        }
        const temp = {
          fsiteNo: queryFsiteNo,
          fpointFlag: 4,
          page: this.listQuerySearchlab.page,
          limit: this.listQuerySearchlab.limit,
          sort: "create_time",
          order: "desc",
          fpointName: this.listQuerySearchlab.fpointName,
        };
        listRoom(temp)
          .then((response) => {
            this.searchListlab = response.data.data.list;
            this.totalSearchlab = response.data.data.total;
            this.listLoadingSearchlab = false;
          })
          .catch(() => {
            this.searchListlab = [];
            this.totalSearchlab = 0;
            this.listLoadingSearchlab = false;
          });
      } else {
        this.searchListlab = [];
      }
    },

    // 地点获取搜索列表
    searchFilterlab() {
      this.listQuerySearchlab.page = 1;
      this.getSearchListlab(this.getFsiteNo);
    },
    // 地点获取搜索列表
    searchDatalab(row) {
      this.searchVisiblelab = false;
      this.listQuerySearchlab.page = 1;
      if (this.searchFlaglab === "add") {
        this.dataForm.fpointNo = row.fpointNo;
      } else {
        this.listQuery.fpointNo = row.fpointNo;
        this.printed = false;
        this.isPrint = true;
      }
    },

    // 地点获取搜索列表
    searchDialoglab(data) {
      if (data === "add") {
        this.searchFlaglab = "add";
      } else {
        this.searchFlaglab = "search";
        if (!this.listQuery.fsiteNo) {
          this.searchListlab = [];
        } else {
          this.getSearchListlab(this.listQuery.fsiteNo);
        }
      }
      this.searchVisiblelab = true;
    },

    // 搜索地点获取焦点
    fpointNoFocus() {
      if (!this.listQuery.fsiteNo) {
        this.getListfpointNoQuery = [];
      }
    },

    // 搜索地点改变
    fpointNoChange(data) {
      this.printed = false;
      this.isPrint = true;
      if (data) {
      } else {
        this.listQuery.fpointNo = undefined;
      }
    },

    // 存放地点
    formatfpointNo: function (row, column) {
      if (this.getListfpointNoAll) {
        for (let i = 0; i < this.getListfpointNoAll.length; i++) {
          if (row.fpointNo === this.getListfpointNoAll[i].value) {
            return this.getListfpointNoAll[i].label;
          }
        }
        return "";
      } else {
        return "";
      }
    },
    datetimeFormat(row, column) {
      const date = row[column.property];
      if (date === undefined || date === null) {
        return "";
      }
      return moment(date).format("YYYY-MM-DD");
    },
    uploadFileSuccess: function (response) {
      this.$notify.success({
        title: "导入",
        message: response.data,
      });
    },

    // 设备类型
    equipmenttype: function (fequipmenttype) {
      if (this.getListfequipmenttype.length > 0) {
        for (let i = 0; i < this.getListfequipmenttype.length; i++) {
          if (fequipmenttype === this.getListfequipmenttype[i].value) {
            return this.getListfequipmenttype[i].label;
          }
        }
        return "";
      } else {
        return "";
      }
    },
    // 设备类型回显
    formatfequipmenttype: function (row) {
      if (this.searchListAll.length > 0) {
        for (let i = 0; i < this.searchListAll.length; i++) {
          if (row.feuipmentid === this.searchListAll[i].feuipmentid) {
            return this.equipmenttype(this.searchListAll[i].fequipmenttype);
          }
        }
        return "";
      } else {
        return "";
      }
    },
    // 编辑设备类型回显
    formatfetype: function (row) {
      if (this.searchListAll.length > 0) {
        for (let i = 0; i < this.searchListAll.length; i++) {
          if (row.feuipmentid === this.searchListAll[i].feuipmentid) {
            return this.searchListAll[i].fequipmenttype;
          }
        }
        return "";
      } else {
        return "";
      }
    },

    // 型号回显
    formatfspec(row) {
      if (this.searchListAll.length > 0) {
        for (let i = 0; i < this.searchListAll.length; i++) {
          if (row === this.searchListAll[i].feuipmentid) {
            return this.searchListAll[i].fspec;
          }
        }
        return "";
      } else {
        return "";
      }
    },
    // 设备分类
    formatfclassstno: function (row, column) {
      if (this.getListfclassstno.length > 0) {
        for (let i = 0; i < this.getListfclassstno.length; i++) {
          if (row.fclassstno === this.getListfclassstno[i].value) {
            return this.getListfclassstno[i].label;
          }
        }
        return "";
      } else {
        return "";
      }
    },
    // 设备状态
    formatfstate: function (row, column) {
      if (this.fstateOptions.length > 0) {
        for (let i = 0; i < this.fstateOptions.length; i++) {
          if (row.fstate === this.fstateOptions[i].value) {
            return this.fstateOptions[i].label;
          }
        }
        return "";
      } else {
        return "";
      }
    },

    // 搜索弹出框
    searchDialog() {
      this.searchVisible = true;
    },

    // 搜索过滤
    searchFilter() {
      this.listQuerySearch.page = 1;
      this.getSearchList();
    },

    // 搜索数据
    searchData(row) {
      this.searchVisible = false;
      this.dataForm.feuipmentid = row.feuipmentid;
      this.dataForm.fequipmentname = row.fequipmentname;
      this.dataForm.fequipmenttag = row.fequipmentname;
      this.dataForm.fclassstno = row.fclassstno;
      this.dataForm.fequipmenttype = row.fequipmenttype;
      this.dataForm.funit = row.funit;
      this.dataForm.fspec = row.fspec;
      this.dataForm.fuseyear = row.fuseyear;

      getFequipmentbarcode({ feuipmentid: row.fequipmenttype })
        .then((response) => {
          if (response) {
            this.dataForm.fequipmentbarcode = response.data.data;
          }
        })
        .catch(() => {
          this.dataForm.fequipmentbarcode = "";
        });
    },

    // 设备信息列表
    getSearchList() {
      this.listLoadingSearch = true;
      listinfo(this.listQuerySearch)
        .then((response) => {
          this.searchList = response.data.data.list;
          this.totalSearch = response.data.data.total;
          this.listLoadingSearch = false;
        })
        .catch(() => {
          this.searchList = [];
          this.totalSearch = 0;
          this.listLoadingSearch = false;
        });
    },

    getList() {
      this.listLoading = true;
      listequipmentInv(this.listQuery)
        .then((response) => {
          this.list = response.data.data.list;
          this.total = response.data.data.total;
          this.listLoading = false;

          this.isPrint = true;
          this.printed = false;
          if (this.listQuery.fpointNo != null) {
            this.isPrint = false;
            this.list2 = response.data.data.list;
          }
        })
        .catch(() => {
          this.list = [];
          this.total = 0;
          this.listLoading = false;
        });
    },
    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },
    resetForm() {
      this.dataForm = {
        fsiteNo: undefined,
        feuipmentid: undefined,
        fequipmenttag: undefined,
        fpointNo: undefined,
        fpointSize: undefined,
        finstalldate: undefined,
        fequipmenttype: undefined,
        fequipmentbarcode: undefined,
        fstate: undefined,
        fmaintaindate: undefined,
        frepairdate: undefined,
        fbuyprice: undefined,
        foldway: undefined,
        foldmoney: undefined,
        flastrate: undefined,
        flastmoney: undefined,
        flosdate: undefined,
        fvideourl: undefined,
        fmacno: undefined,
        fsupno: undefined,
        flinkmac: undefined,
        flinkmark: undefined,
        fip: undefined,
        fport: undefined,
        fcolor: undefined,
        fmeeting: undefined,
        id: undefined,
      };
    },
    handleCreate() {
      this.resetForm();
      this.dialogStatus = "create";
      if (this.list.length > 0) {
        this.dataForm.fsiteNo = this.list[0].fsiteNo;
        this.getSearchListlab(this.list[0].fsiteNo);
        getPointOptions(this.list[0].fsiteNo)
          .then((response) => {
            this.getListfpointNo = response.data.data.list;
          })
          .catch(() => {
            this.getListfpointNo = [];
          });
        this.getFsiteNo = this.list[0].fsiteNo;
      }
      if (this.$store.state.user.fsiteLevel === 3) {
        this.dataForm.fsiteNo = this.$store.state.user.fsiteNo;
        this.getSearchListlab(this.$store.state.user.fsiteNo);
        getPointOptions(this.$store.state.user.fsiteNo)
          .then((response) => {
            this.getListfpointNo = response.data.data.list;
          })
          .catch(() => {
            this.getListfpointNo = [];
          });
      }
      this.dataForm.fmeeting = 0;
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].clearValidate();
      });
    },
    createData() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.dataForm.foperator = this.$store.state.user.id;
          this.dataForm.fupddate = new Date();
          createequipmentInv(this.dataForm)
            .then((response) => {
              this.list.unshift(response.data.data);
              this.dialogFormVisible = false;
              this.$notify.success({
                title: "成功",
                message: "添加设备台帐成功",
              });
            })
            .catch((response) => {
              this.$notify.error({
                title: "失败",
                message: response,
              });
            });
        }
      });
    },
    handleUpdate(row) {
      this.dataForm = Object.assign({}, row);
      this.getSearchListlab(row.fsiteNo);
      getPointOptions(row.fsiteNo)
        .then((response) => {
          this.getListfpointNo = response.data.data.list;
        })
        .catch(() => {
          this.getListfpointNo = [];
        });
      this.dataForm.fequipmenttype = this.formatfetype(row);
      this.dataForm.fspec = this.formatfspec(row.feuipmentid);

      this.dialogStatus = "update";
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].clearValidate();
      });
    },
    updateData() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.dataForm.foperator = this.$store.state.user.id;
          this.dataForm.fupddate = new Date();
          updateequipmentInv(this.dataForm, this.$route.query.id)
            .then(() => {
              for (const v of this.list) {
                if (v.fequipmentbarcode === this.dataForm.fequipmentbarcode) {
                  const index = this.list.indexOf(v);
                  this.list.splice(index, 1, this.dataForm);
                  break;
                }
              }
              this.dialogFormVisible = false;
              this.$notify.success({
                title: "成功",
                message: "更新设备台帐成功",
              });
            })
            .catch((response) => {
              this.$notify.error({
                title: "失败",
                message: response,
              });
            });
        }
      });
    },
    handleDelete(row) {
      this.$confirm("是否要删除设备台账", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        deleteequipmentInv(row)
          .then((response) => {
            this.$notify.success({
              title: "成功",
              message: "删除设备台账成功",
            });
            const index = this.list.indexOf(row);
            this.list.splice(index, 1);
          })
          .catch((response) => {
            this.$notify.error({
              title: "失败",
              message: response,
            });
          });
      });
    },
    indexMethod(index) {
      index = index + 1;
      return index;
    },
  },
};
</script>